package com.flyme.module.base.provider.service.impl;

import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysCertificate;
import com.flyme.module.base.client.entity.SysUsercertificate;
import com.flyme.module.base.provider.mapper.SysCertificateMapper;
import com.flyme.module.base.provider.service.SysCertificateService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysCertificateServiceImpl extends BaseServiceImpl<SysCertificateMapper, SysCertificate> implements SysCertificateService {


    @Override
    public Boolean edit(SysCertificate SysCertificate) {
        return saveOrUpdate(SysCertificate);
    }

    @Override
    public EntityMap selectList(){
        List<SysCertificate> list = selectAll();
        EntityMap map = new EntityMap();
        for(SysCertificate certificate: list){
            map.put(certificate.getCertificateCode(), certificate.getId());
        }
        return map;
    }

    @Override
    public SysCertificate findByCode(String certificateCode){
        return getOne(q().eq("certificateCode", certificateCode), false);
    }


    /**
     * 根据code统计分数
     */
    @Override
    public String countScoreByCode(String certificateCode){
        CriteriaQuery<EntityMap> certificate = new CriteriaQuery();
        certificate.select("IFNULL((SELECT SUM(tc.transferCredit) FROM sys_transfercourse tc WHERE tc.certificateId = c.id), 0) score");
        certificate.eq("certificateCode",certificateCode);
        EntityMap score = getEntityMap(certificate);
        return score.get("score").toString();
    }
}
